#!/usr/bin/env bash
if [[ $1 == "-h" ]]; then
    h h laji
    exit
fi
echo "本脚本用于删除一些不重要的临时文件或垃圾文件。"
h h laji

read -r -p "请输入您的选择：" args1

case $args1 in
    0)
        h h laji;;
    1)
        sed -i -e '/^find_strings=/c find_strings=' -e '/^replace_strings=/c replace_strings=' -e '/^lastfolderopen=/c lastfolderopen=' -e '/^recentfiles=/c recentfiles=@Invalid()' $HOME/.local/share/sigil-ebook/sigil/sigil_v6.ini
        echo "成功删除sigil历史记录。"
        sed -i "/\[Recent Files\]/,/^$/d" $HOME/.config/gwenviewrc
        res=`ls -A $HOME/.local/share/gwenview/recentfolders/`
        if [[ -n $res ]];then
            rm $HOME/.local/share/gwenview/recentfolders/*
        fi
        echo "成功删除gwenview历史记录。"
        sed -i "/\[Recent Files\]/,/^$/d" $HOME/.config/okularrc
        echo "成功删除okular历史记录。"
        sed -i -r 's/^(Files\\Recent%20Files=|Search\\Find%20History=|Search\\Replace%20History=).*/\1@Invalid()/g' $HOME/.config/texstudio/texstudio.ini
        echo "成功删除texstudio历史记录"
        if sed -i -e '/^list=/c list=@Invalid()' -e '/^items\\/d' -e '/^filename=/a items\\size=0'  $HOME/.config/vlc/vlc-qt-interface.conf;then
            echo "成功删除vlc历史记录。"
        fi
        if sed -i -e '/^recents=/c recents=@Invalid()' -e '/^urls=/c urls=@Invalid()' -e "/^latest_dir=/c latest_dir=$HOME\/Videos"  $HOME/.config/smplayer/smplayer.ini;then
            sed -i -r -e "/^latest_dir=/c latest_dir=$HOME\/Videos" -e '/^items\\[0-9]/d' -e '/^items\\size=/s/[0-9]+/0/g' $HOME/.config/smplayer/playlist.ini
            echo "成功删除smplayer历史记录。"
        fi
        ;;
    2)
        echo -e "\e[33m"
        ls *.soc *.loc *.xdv *.synctex.gz *.log *.aux *.dvi *.lof *.lot *.bit *.idx *.glo *.bbl *.bcf *.ilg *.toc *.ind *.out *.blg *.fdb_latexmk *.fls *.run.xml 2>/dev/null
        echo -e "\e[0m"
        read -r -p "将要删除如上文件，请问是否继续？[y/n]" input
        if [[ $input != "y" ]]; then
            echo "您已放弃删除。"
            exit 0
        fi
        for i in {*.soc,*.loc,*.xdv,*.synctex.gz,*.log,*.aux,*.dvi,*.lof,*.lot,*.bit,*.idx,*.glo,*.bbl,*.bcf,*.ilg,*.toc,*.ind,*.out,*.blg,*.fdb_latexmk,*.fls,*.run.xml}; do
            find . -name $i -type f -delete 
        done
        echo "成功删除latex临时文件。"
        ;;

    "")
        echo "您已放弃此次操作。"
        ;;
esac

unset args1 input
